<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
			<el-form-item label="档案区编号" prop="areaNo">
				<el-tree-select v-model="queryParams.areaNo" :data="areaOptions" :props="{ value: 'archAreaNo', label: 'label', children: 'children' }"
				 value-key="archAreaNo" placeholder="请选择档案区" clearable @keyup.enter="handleQuery" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
				<el-button icon="Refresh" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<!-- <el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['environment:environment:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['environment:environment:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['environment:environment:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['environment:environment:export']">导出</el-button>
			</el-col>
			<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
		</el-row> -->

		<el-table v-loading="loading" :data="environmentList" @selection-change="handleSelectionChange">
			<!-- <el-table-column type="selection" width="55" align="center" /> -->
			<el-table-column label="档案区" align="center" prop="areaNo" />
			<el-table-column label="当前温度" align="center" prop="nowTemperature" />
			<el-table-column label="当前湿度" align="center" prop="nowHumidity" />
			<el-table-column label="更新时间" align="center" prop="updatetime" width="180">
				<template #default="scope">
					<span>{{ parseTime(scope.row.updatetime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
				</template>
			</el-table-column>
			<el-table-column label="红外线" align="center" prop="infrared">
				<template v-slot="scope">
					<span>{{ parseInt(scope.row.infrared, 10) === 100 ? '报警' : '正常' }}</span>
				</template>
			</el-table-column>
			<el-table-column label="水浸100" align="center" prop="water">
				<template v-slot="scope">
					<span>{{ parseInt(scope.row.water, 10) === 100 ? '报警' : '正常' }}</span>
				</template>
			</el-table-column>
			<el-table-column label="烟雾100" align="center" prop="smoke">
				<template v-slot="scope">
					<span>{{ parseInt(scope.row.smoke, 10) === 100 ? '报警' : '正常' }}</span>
				</template>
			</el-table-column>
			<el-table-column label="PM10" align="center" prop="pm10" />
			<el-table-column label="PM2.5" align="center" prop="pm25" />
			<el-table-column label="CO2" align="center" prop="co2" />
			<el-table-column label="TVOC" align="center" prop="tvoc" />
			<el-table-column label="甲醛" align="center" prop="formaldehyde" />
			<!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template #default="scope">
					<el-button type="text" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['environment:environment:edit']">修改</el-button>
					<el-button type="text" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['environment:environment:remove']">删除</el-button>
				</template>
			</el-table-column> -->
		</el-table>

		<pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
		 @pagination="getList" />

		<!-- 添加或修改环境对话框 -->
		<el-dialog :title="title" v-model="open" width="500px" append-to-body>
			<el-form ref="environmentRef" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="档案区" prop="areaNo">
					<el-input v-model="form.areaNo" placeholder="请输入库房id" />
				</el-form-item>
				<el-form-item label="当前温度" prop="nowTemperature">
					<el-input v-model="form.nowTemperature" placeholder="请输入当前温度" />
				</el-form-item>
				<el-form-item label="当前湿度" prop="nowHumidity">
					<el-input v-model="form.nowHumidity" placeholder="请输入当前湿度" />
				</el-form-item>
				<el-form-item label="红外线" prop="infrared">
					<el-input v-model="form.infrared" placeholder="请输入红外线" />
				</el-form-item>
				<el-form-item label="水浸100" prop="water">
					<el-input v-model="form.water" placeholder="请输入水浸100" />
				</el-form-item>
				<el-form-item label="烟雾100" prop="smoke">
					<el-input v-model="form.smoke" placeholder="请输入烟雾100" />
				</el-form-item>
				<el-form-item label="PM10" prop="pm10">
					<el-input v-model="form.pm10" placeholder="请输入PM10" />
				</el-form-item>
				<el-form-item label="PM2.5" prop="pm25">
					<el-input v-model="form.pm25" placeholder="请输入PM2.5" />
				</el-form-item>
			</el-form>
			<template #footer>
				<div class="dialog-footer">
					<el-button type="primary" @click="submitForm">确 定</el-button>
					<el-button @click="cancel">取 消</el-button>
				</div>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="Environment">
	import {
		listEnvironment,
		getEnvironment,
		delEnvironment,
		addEnvironment,
		updateEnvironment
	} from "@/api/environment/environment";
	import {
		listArchiveArchAreaAll
	} from "@/api/archive/ArchiveArchArea";
	const {
		proxy
	} = getCurrentInstance();

	const environmentList = ref([]);
	const open = ref(false);
	const loading = ref(true);
	const showSearch = ref(true);
	const ids = ref([]);
	const single = ref(true);
	const multiple = ref(true);
	const total = ref(0);
	const title = ref("");
	const areaOptions = ref([]);

	const data = reactive({
		form: {},
		queryParams: {
			pageNum: 1,
			pageSize: 10,
			areaNo: null,
		},
		rules: {}
	});

	const {
		queryParams,
		form,
		rules
	} = toRefs(data);

	/** 查询环境列表 */
	function getList() {
		loading.value = true;
		listEnvironment(queryParams.value).then(response => {
			environmentList.value = response.rows;
			total.value = response.total;
			loading.value = false;
		});
	}

	// 取消按钮
	function cancel() {
		open.value = false;
		reset();
	}

	// 表单重置
	function reset() {
		form.value = {
			environmentId: null,
			areaNo: null,
			nowHumidity: null,
			nowTemperature: null,
			updatetime: null,
			infrared: null,
			water: null,
			smoke: null,
			pm10: null,
			pm25: null
		};
		proxy.resetForm("environmentRef");
	}

	/** 搜索按钮操作 */
	function handleQuery() {
		queryParams.value.pageNum = 1;
		getList();
	}

	/** 重置按钮操作 */
	function resetQuery() {
		proxy.resetForm("queryRef");
		handleQuery();
	}

	// 多选框选中数据
	function handleSelectionChange(selection) {
		ids.value = selection.map(item => item.environmentId);
		single.value = selection.length != 1;
		multiple.value = !selection.length;
	}

	/** 新增按钮操作 */
	function handleAdd() {
		reset();
		open.value = true;
		title.value = "添加环境";
	}

	/** 修改按钮操作 */
	function handleUpdate(row) {
		reset();
		const environmentId = row.environmentId || ids.value
		getEnvironment(environmentId).then(response => {
			form.value = response.data;
			open.value = true;
			title.value = "修改环境";
		});
	}

	/** 提交按钮 */
	function submitForm() {
		proxy.$refs["environmentRef"].validate(valid => {
			if (valid) {
				if (form.value.environmentId != null) {
					updateEnvironment(form.value).then(response => {
						proxy.$modal.msgSuccess("修改成功");
						open.value = false;
						getList();
					});
				} else {
					addEnvironment(form.value).then(response => {
						proxy.$modal.msgSuccess("新增成功");
						open.value = false;
						getList();
					});
				}
			}
		});
	}

	/** 删除按钮操作 */
	function handleDelete(row) {
		const environmentIds = row.environmentId || ids.value;
		proxy.$modal.confirm('是否确认删除环境编号为"' + environmentIds + '"的数据项？').then(function() {
			return delEnvironment(environmentIds);
		}).then(() => {
			getList();
			proxy.$modal.msgSuccess("删除成功");
		}).catch(() => {});
	}

	/** 导出按钮操作 */
	function handleExport() {
		proxy.download('environment/environment/export', {
			...queryParams.value
		}, `environment_${new Date().getTime()}.xlsx`)
	}
	/** 查询档案区下拉树结构 */
	function getListArchiveArchArea() {
		listArchiveArchAreaAll().then(response => {
			areaOptions.value = response.rows;
		});
	};

	/** 初始化档案区数据 */
	function initAreaOptions() {
		// 判断档案区的数据是否存在，存在不获取，不存在则获取
		if (areaOptions.value.length === 0) {
			listArchiveArchAreaAll().then(response => {
				areaOptions.value = response.rows;
			});
		}
	};

	initAreaOptions();
	getList();
</script>
